package com;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import android.content.Context;
import android.text.TextUtils;

import com.ciko.code.constant.Api;
import com.ciko.code.constant.Constants;
import com.shwread.http.base.AsyncHttpClient;
import com.shwread.http.base.RequestParams;
import com.shwread.http.base.ResponseHandlerInterface;
import com.shwread.http.util.Base64;
import com.shwread.http.util.LogLevel;
import com.shwread.http.util.Logger;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import java.util.ArrayList;

public class TrainActionHelp {
    private static String ENTERPRISE_ID = Constants.enterprise_num;
    private static final String URL_R = Api.ROOT_URL;
    private static String LOGIN_KEY = "LQSW@)!$";
    public static final String CONTENTTYPE = "application/xml;charset=UTF-8";
    public static final String UPLOAD_CONTENTTYPE = "application/octet-stream";
    private static final String CHARTSET = "UTF-8";
    private static String ACCOUNTTYPE = "1";
    private static String CLIENTTYPE = "android_1.0.0";
    private static String CLIENT_IMEI;
    private static String USER_IP;
    private static String ACCOUNT;
    private static String URL;
    private static boolean ISRELEASE;
    private static AsyncHttpClient client = new AsyncHttpClient();

    public TrainActionHelp() {
    }

    public static void init(String imei, String psdnIp, boolean isRelease) {
        CLIENT_IMEI = TextUtils.isEmpty(imei)?"":imei;
        USER_IP = TextUtils.isEmpty(psdnIp)?"":psdnIp;
        ISRELEASE = isRelease;
        URL = URL_R;
        Logger.init(ISRELEASE?LogLevel.NONE:LogLevel.FULL);
    }

    public static String getEnterpriseId() {
        return ENTERPRISE_ID;
    }

    public static void setEnterpriseId(String enterpriseId) {
        ENTERPRISE_ID = enterpriseId;
    }

    public static String getAccountType() {
        return ACCOUNTTYPE;
    }

    public static void setAccountType(String accountType) {
        ACCOUNTTYPE = accountType;
    }

    public static String getClientType() {
        return CLIENTTYPE;
    }

    public static void setClientType(String clientType) {
        CLIENTTYPE = clientType;
    }

    public static String getAccount() {
        return ACCOUNT;
    }

    public static void setAccount(String account) {
        ACCOUNT = account;
    }

    public static String getUrl() {
        return URL;
    }

    public static String getLoginKey() {
        return LOGIN_KEY;
    }

    public static void setLoginKey(String key) {
        if(!TextUtils.isEmpty(key)) {
            LOGIN_KEY = key;
        }

    }

    public static void setDefaultLoginKey() {
        LOGIN_KEY = "LQSW@)!$";
    }

    public static void cancel(Context context) {
        if(client != null && context != null) {
            client.cancelRequests(context);
        }

    }

    public static void cancelAll() {
        if(client != null) {
            client.cancelAllRequests(true);
        }

    }

    public static void destroy() {
        if(client != null) {
            client.cancelAllRequests(true);
            client = null;
        }

        CLIENT_IMEI = null;
        USER_IP = null;
    }

    private static RequestParams getRequestParams(String timeStamp) {
        RequestParams params = new RequestParams();
        params.put("accountType", ACCOUNTTYPE);
        params.put("clientType", CLIENTTYPE);
        params.put("enterpriseId", ENTERPRISE_ID);
        if(!TextUtils.isEmpty(ACCOUNT)) {
            params.put("account", ACCOUNT);
        }

        params.put("timeStamp", timeStamp);
        return params;
    }

    private static RequestParams getRequestParams(String timeStamp, String account) {
        RequestParams params = new RequestParams();
        params.put("accountType", ACCOUNTTYPE);
        params.put("clientType", CLIENTTYPE);
        params.put("enterpriseId", ENTERPRISE_ID);
        if(!TextUtils.isEmpty(account)) {
            params.put("account", account);
        }

        params.put("timeStamp", timeStamp);
        return params;
    }

    public static void qryTrainIndexForClient(Context context, String isOfficialVersion, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("isOfficialVersion", isOfficialVersion);
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID));
        client.post(context, getUrl("trainIndexForClient.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    public static void qryTrainIndexBanner(Context context, String cid, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("cid", cid);
        client.post(context, getUrl("queryTrainBannerList.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    public static void qryTrainList(Context context, int type, String cid, int pageNum, int lenght, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("type", type);
        params.put("cid", cid);
        params.put("pageNum", pageNum);
        params.put("length", lenght);
        client.post(context, getUrl("qryTrainList.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    public static void qryExamList(Context context, int type, String cid, int pageNum, int lenght, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("type", type);
        params.put("cid", cid);
        params.put("pageNum", pageNum);
        params.put("length", lenght);
        client.post(context, getUrl("queryExamList.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    public static void qryMyExamList(Context context, String cid, int pageNum, int lenght, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("cid", cid);
        params.put("pageNum", pageNum);
        params.put("length", lenght);
        client.post(context, getUrl("queryMyExamList.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    public static void qryUserStudyRecord(Context context, int type, String cid, int pageNum, int lenght, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("type", type);
        params.put("cid", cid);
        params.put("pageNum", pageNum);
        params.put("length", lenght);
        client.post(context, getUrl("queryUserStudyRecord.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    public static void qryTrainSearchList(Context context, String trainName, String cid, int pageNum, int length, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("trainName", trainName);
        params.put("cid", cid);
        params.put("pageNum", pageNum);
        params.put("length", length);
        client.post(context, getUrl("qryTrainSearchList.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    public static void addStudyChapterLog(Context context, String cid, int trainId, int chapterId, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("cid", cid);
        params.put("trainId", trainId);
        params.put("chapterId", chapterId);
        client.post(context, getUrl("addStudyChapterLog.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    public static void qryDocumentColumnInfo(Context context, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("cid", cid3(timeStamp));
        client.post(context, getUrl("qryDocumentColumnInfo.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    public static void qryDocumentInfolist(Context context, long columnId, int documentTyep, int lastOrderNum, int length, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("columnId", String.valueOf(columnId));
        params.put("documentTyep", String.valueOf(documentTyep));
        params.put("lastOrderNum", String.valueOf(lastOrderNum));
        params.put("length", String.valueOf(length));
        params.put("cid", cid3(timeStamp + columnId + documentTyep + lastOrderNum + length));
        client.post(context, getUrl("qryDocumentInfolist.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    private static String getUrl(String action) {
        return URL + action;
    }

    private static String cid(String value) {
        return Base64.base64Encode(value, LOGIN_KEY, "UTF-8");
    }

    private static String cid3(String value) {
        StringBuffer sb = (new StringBuffer(ACCOUNT)).append(ACCOUNTTYPE).append(CLIENTTYPE);
        return cid(sb.append(value).toString());
    }

    private static Header[] getHeaders(Context context) {
        ArrayList headers = new ArrayList();
        headers.add(new BasicHeader("Connection", "Keep-Alive"));
        headers.add(new BasicHeader("Chartset", "UTF-8"));
        headers.add(new BasicHeader("Cache-Control", "no-cache"));
        headers.add(new BasicHeader("Accept-Language", "zh-CN,en-US"));
        headers.add(new BasicHeader("client-imei", CLIENT_IMEI));
        headers.add(new BasicHeader("user-ip", USER_IP));
        if(!TextUtils.isEmpty(ACCOUNT)) {
            headers.add(new BasicHeader("phone-number", ACCOUNT));
        }

        return (Header[])headers.toArray(new Header[headers.size()]);
    }

    private static Header[] getHeaders(Context context, String account) {
        ArrayList headers = new ArrayList();
        headers.add(new BasicHeader("Connection", "Keep-Alive"));
        headers.add(new BasicHeader("Chartset", "UTF-8"));
        headers.add(new BasicHeader("Cache-Control", "no-cache"));
        headers.add(new BasicHeader("Accept-Language", "zh-CN,en-US"));
        headers.add(new BasicHeader("client-imei", CLIENT_IMEI));
        headers.add(new BasicHeader("user-ip", USER_IP));
        if(!TextUtils.isEmpty(account)) {
            headers.add(new BasicHeader("phone-number", account));
        }

        return (Header[])headers.toArray(new Header[headers.size()]);
    }


    public static void getAttendByUser(Context context, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("isNew", "y");
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID));
        client.post(context, getUrl("getAttendByUser.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }
    public static void getMonthAttendsByUser(Context context,long time, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("attendMonth", time);
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID));
        client.post(context, getUrl("getMonthAttendsByUser.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }

    //学校ui改造新加的banner接口----------->1.1.	学生天地|分享模块banner位查询  qryBannerListByPosition.do   7=学生天地 8=分享模块
    public static void qryBannerListByPosition(Context context,int position, ResponseHandlerInterface handler) {
        String timeStamp = String.valueOf(System.currentTimeMillis());
        RequestParams params = getRequestParams(timeStamp);
        params.put("position", position+"");
        params.put("cid", cid3(timeStamp + ENTERPRISE_ID));
        client.post(context, getUrl("qryBannerListByPosition.do"), getHeaders(context), params, "application/xml;charset=UTF-8", handler);
    }
}

